package com.example.g32.park

import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.paging.PagingDataAdapter
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.example.base.entity.ParkEntityItem
import com.example.g32.R
import com.example.g32.databinding.ItemParkBinding

class ParkAdapter:PagingDataAdapter<ParkEntityItem, ParkAdapter.ParkViewHolder>(diff) {
    private var mOnItemClickListener: OnItemClickListener? = null

    private var mOnItemLongClickListener:OnItemLongClickListener?=null

    object diff : DiffUtil.ItemCallback<ParkEntityItem>() {
        override fun areItemsTheSame(oldItem: ParkEntityItem, newItem: ParkEntityItem): Boolean {
            return oldItem == newItem
        }

        override fun areContentsTheSame(oldItem: ParkEntityItem, newItem: ParkEntityItem): Boolean {
            return oldItem.id == newItem.id
        }

    }


    class ParkViewHolder(view:View):ViewHolder(view){
        val binding=DataBindingUtil.bind<ItemParkBinding>(view)!!
    }

    override fun onBindViewHolder(holder: ParkViewHolder, position: Int) {
        val item = getItem(position)!!
        holder.binding.bean=item
        holder.binding.parkStatus.apply {
            when(item.spaceStatus){
                "空闲"->{
                    setTextColor(context.getColor(R.color.green))
                    setBackgroundResource(R.drawable.shape_status)
                }
                "已占用"->{
                    setTextColor(Color.BLUE)
                    setBackgroundResource(R.drawable.shape_zhanyong)
                }
                else->{
                    setTextColor(context.getColor(R.color.orange))
                    setBackgroundResource(R.drawable.shape_weihu)
                }
            }
        }

        holder.itemView.setOnLongClickListener {
            mOnItemLongClickListener?.onitemLongClick(position)!!
        }

        holder.itemView.setOnClickListener {
            mOnItemClickListener?.onItemClick(position, item)
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ParkViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_park, parent, false)
        return ParkViewHolder(view)
    }

    fun setOnItemLongClickListener(listener: OnItemLongClickListener){
        this.mOnItemLongClickListener=listener
    }

      fun setOnItemClickListener(listener: OnItemClickListener?) {
        this.mOnItemClickListener = listener
    }

    }

interface OnItemLongClickListener{
    fun onitemLongClick(position: Int):Boolean
}

interface OnItemClickListener {
    fun onItemClick(position: Int, item:ParkEntityItem)
}
